/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.api.mgrActionAssemblerGen;

import com.inspur.edp.bef.api.attr.AssemblerMethodAttribute;
import com.inspur.edp.bef.api.be.IBEManagerContext;
import com.inspur.edp.bef.api.parameter.retrieve.RetrieveParam;
import com.inspur.edp.bef.bizentity.operation.BizMgrAction;
import com.inspur.edp.bef.bizentity.operation.internalmgraction.DeleteMgrAction;
import com.inspur.edp.bef.bizentity.operation.internalmgraction.ModifyMgrAction;
import com.inspur.edp.bef.bizentity.operation.internalmgraction.QueryMgrAction;
import com.inspur.edp.bef.bizentity.operation.internalmgraction.RetrieveDefaultMgrAction;
import com.inspur.edp.bef.bizentity.operation.internalmgraction.RetrieveMgrAction;
import com.inspur.edp.bef.spi.action.assembler.delete.DeleteMgrActionAssembler;
import com.inspur.edp.bef.spi.action.assembler.mgrAssemblerFactory.IDefaultMgrActionAssFactory;
import com.inspur.edp.bef.spi.action.assembler.modify.ModifyMgrActionAssembler;
import com.inspur.edp.bef.spi.action.assembler.query.QueryMgrActionAssembler;
import com.inspur.edp.bef.spi.action.assembler.retrieve.RetrieveMgrActionAssembler;
import com.inspur.edp.bef.spi.action.assembler.retrieveDefault.RetrieveDefaultMgrActionAssembler;
import com.inspur.edp.caf.generator.baseInfo.AnnotationInfo;
import com.inspur.edp.caf.generator.baseInfo.AnnotationType;
import com.inspur.edp.caf.generator.baseInfo.InterfaceMethodGenerateInfo;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.InterfaceMethodGenerator;
import com.inspur.edp.caf.generator.method.InterfaceMethodGeneratorContext;
import com.inspur.edp.cef.entity.changeset.IChangeDetail;
import com.inspur.edp.cef.entity.condition.EntityFilter;
import com.inspur.edp.cef.generator.overall.Utils;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.StringLiteral;

public class InternalMgrActionMethodGenerator extends InterfaceMethodGenerator {

  // region Basic Info
  private BizMgrAction mgrAction;

  public InternalMgrActionMethodGenerator(BizMgrAction mgrAction) {
    this.mgrAction = mgrAction;
  }

  // region Static Members
  public static String getMethodName(BizMgrAction mgrAction) {
    if (mgrAction instanceof QueryMgrAction) {
      return "getQueryMgrActionAssembler";
    }
    if (mgrAction instanceof RetrieveDefaultMgrAction) {
      return "getRetrieveDefaultMgrActionAssembler";
    }
    if (mgrAction instanceof RetrieveMgrAction) {
      return "getRetrieveMgrActionAssembler";
    }
    if (mgrAction instanceof ModifyMgrAction) {
      return "getModifyMgrActionAssembler";
    }
    if (mgrAction instanceof DeleteMgrAction) {
      return "getDeleteMgrActionAssembler";
    }
    throw new IllegalArgumentException();
  }

  public static java.util.ArrayList<ParameterInfo> getMethodParameters(BizMgrAction mgrAction) {
    if (mgrAction
        instanceof QueryMgrAction) // IBEManagerContext IBEManagerContext, EntityFilter entityFilter
    {
      ParameterInfo tempVar = new ParameterInfo();
      tempVar.setParamName("IBEManagerContext");
      tempVar.setParamType(
          com.inspur.edp.cef.generator.overall.Utils.createTypeInfo(IBEManagerContext.class));
      ParameterInfo tempVar2 = new ParameterInfo();
      tempVar2.setParamName("entityFilter");
      tempVar2.setParamType(
          com.inspur.edp.cef.generator.overall.Utils.createTypeInfo(EntityFilter.class));
      return new java.util.ArrayList<ParameterInfo>(java.util.Arrays.asList(tempVar, tempVar2));
    }
    if (mgrAction instanceof RetrieveDefaultMgrAction) {
      ParameterInfo tempVar3 = new ParameterInfo();
      tempVar3.setParamName("IBEManagerContext");
      tempVar3.setParamType(
          com.inspur.edp.cef.generator.overall.Utils.createTypeInfo(IBEManagerContext.class));
      return new java.util.ArrayList<ParameterInfo>(java.util.Arrays.asList(tempVar3));
    }
    if (mgrAction
        instanceof RetrieveMgrAction) // IBEManagerContext IBEManagerContext, RetrieveParam para
    {
      ParameterInfo tempVar4 = new ParameterInfo();
      tempVar4.setParamName("IBEManagerContext");
      tempVar4.setParamType(
          com.inspur.edp.cef.generator.overall.Utils.createTypeInfo(IBEManagerContext.class));
      ParameterInfo tempVar5 = new ParameterInfo();
      tempVar5.setParamName("para");
      tempVar5.setParamType(
          com.inspur.edp.cef.generator.overall.Utils.createTypeInfo(RetrieveParam.class));
      return new java.util.ArrayList<ParameterInfo>(java.util.Arrays.asList(tempVar4, tempVar5));
    }
    if (mgrAction instanceof ModifyMgrAction) {
      ParameterInfo tempVar6 = new ParameterInfo();
      tempVar6.setParamName("IBEManagerContext");
      tempVar6.setParamType(
          com.inspur.edp.cef.generator.overall.Utils.createTypeInfo(IBEManagerContext.class));
      ParameterInfo tempVar7 = new ParameterInfo();
      tempVar7.setParamName("changeDetail");
      tempVar7.setParamType(
          com.inspur.edp.cef.generator.overall.Utils.createTypeInfo(IChangeDetail.class));
      return new java.util.ArrayList<ParameterInfo>(java.util.Arrays.asList(tempVar6, tempVar7));
    }
    if (mgrAction instanceof DeleteMgrAction) {
      ParameterInfo tempVar8 = new ParameterInfo();
      tempVar8.setParamName("IBEManagerContext");
      tempVar8.setParamType(
          com.inspur.edp.cef.generator.overall.Utils.createTypeInfo(IBEManagerContext.class));
      return new java.util.ArrayList<ParameterInfo>(java.util.Arrays.asList(tempVar8));
    }
    throw new IllegalArgumentException();
  }

  public static java.lang.Class getReturnType(BizMgrAction mgrAction) {
    if (mgrAction instanceof QueryMgrAction) {
      return QueryMgrActionAssembler.class;
    }
    if (mgrAction instanceof RetrieveDefaultMgrAction) {
      return RetrieveDefaultMgrActionAssembler.class;
    }
    if (mgrAction instanceof RetrieveMgrAction) {
      return RetrieveMgrActionAssembler.class;
    }
    if (mgrAction instanceof ModifyMgrAction) {
      return ModifyMgrActionAssembler.class;
    }
    if (mgrAction instanceof DeleteMgrAction) {
      return DeleteMgrActionAssembler.class;
    }
    throw new IllegalArgumentException();
  }

  @Override
  protected InterfaceMethodGeneratorContext createClassInfoContext() {
    return new InterfaceMethodGeneratorContext();
  }

  @Override
  protected InterfaceMethodGenerateInfo createClassMethodInfo() {
    return new InterfaceMethodGenerateInfo();
  }

  @Override
  protected java.util.ArrayList<ModifierKeyword> getAccessModifiers() {
    return new java.util.ArrayList<ModifierKeyword>();
  }

  @Override
  protected String getMethodName() {
    return getMethodName(mgrAction);
  }

  @Override
  protected java.util.ArrayList<ParameterInfo> getParameterCollection() {
    return getMethodParameters(mgrAction);
  }

  // endregion

  @Override
  protected TypeInfo getReturnType() {
    return Utils.createTypeInfo(getReturnType(mgrAction));
  }

  @Override
  protected java.util.ArrayList<AnnotationInfo> getAttributeList() {
    java.util.ArrayList<AnnotationInfo> tempVar = new java.util.ArrayList<AnnotationInfo>(1);
    AnnotationInfo info = new AnnotationInfo();
    info.setTypeInfo(
        com.inspur.edp.cef.generator.overall.Utils.createTypeInfo(AssemblerMethodAttribute.class));
    info.setAnnotationType(AnnotationType.SingleMember);
    StringLiteral valueLiteral = ast.newStringLiteral();
    valueLiteral.setLiteralValue(getAssemblerMethodName());
    info.setSingleParam(valueLiteral);
    tempVar.add(info);
    return tempVar;
  }

  private String getAssemblerMethodName() {
    AssemblerMethodAttribute attr =
        getMethod(getMethodName(mgrAction)).getAnnotation(AssemblerMethodAttribute.class);
    return attr.value();
  }

  private Method getMethod(String name) {
    return Arrays.stream(IDefaultMgrActionAssFactory.class.getMethods())
        .filter(i -> i.getName() == name)
        .findFirst()
        .get();
  }

  // endregion
}
